package com.titaniume.activitiweb;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 *@program: activiti7_workflow
 *@description: 流程实例
 *@author: renfei.huang
 *@create: 2021-11-09 14:15
 */
@SpringBootTest
public class Part3_ProcessInstance {

    //运行
    @Autowired
    private RuntimeService runtimeService;

    // 初始化流程实例 会插入数据到act_ru_execution
    @Test
    public void initProcessInstance() {
        //myProcess_1:bpmn的id
        //bkey001:
        //1.获取页面表填报的内容，请假时间，请假事由，String FromData
        //2.FromData写入业务数表，返回业务表主键ID == businessKey
        //3.把业务数据与Activiti7流程数据进行关联
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_UEL_V2", "bkey002");
        System.out.println("流程定义的ID: " + processInstance.getProcessDefinitionId()); // myProcess_1:3:19ad4316-4110-11ec-9ac9-8cec4b5c0c44
    }



    //获取流程实例列表
    @Test
    public void getProcessInstance() {
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().list();
        for (ProcessInstance pi : list) {
            System.out.println("------------流程实例-----------");
            System.out.println("ProcessInstanceId:" + pi.getProcessInstanceId());
            System.out.println("ProcessDefinitionId:" + pi.getProcessDefinitionId());
            System.out.println("isEnded:" + pi.isEnded());
            System.out.println("isSuspended:" + pi.isSuspended());
        }
    }


    //暂停与激活流程实例
    @Test
    public void activitieProcessInstance() {
//        runtimeService.suspendProcessInstanceById("c4a83292-4127-11ec-a202-8cec4b5c0c44");
//        System.out.println("挂起流实例");


        runtimeService.activateProcessInstanceById("c4a83292-4127-11ec-a202-8cec4b5c0c44");
        System.out.println("激活流实例");
    }

    //删除流程实例
    @Test
    public  void delProcessInstance(){
        runtimeService.deleteProcessInstance("bd618724-42b2-11ec-b8a1-8cec4b5c0c44","排他");
        System.out.println("删除流程实例");
    }
}
